/* 
 * Copyright 2011 kOSMik.
 * This file is part of kOSMik.
 * 
 * kOSMik is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * kOSMik is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with kOSMik.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * kOSMik:
 * Open Street Map - Editor.
 * http://code.google.com/p/swp-dv-ws2010-osm-2/
 */

package de.fuberlin.kosmik.gui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import de.fuberlin.kosmik.R;
import de.fuberlin.kosmik.data.DataBaseConnector;
import de.fuberlin.kosmik.data.DataBaseConnectorImpl;
import de.fuberlin.kosmik.files.MediaController;
import de.fuberlin.kosmik.log.Constants;

/**
 * Activity is used to enter a note to a given point in the track.
 * 
 * @author Michael Schmidt
 */
public class NoteActivity extends Activity implements OnClickListener {
	/**
	 * Intent parameter name of point id, which is associated with the note.
	 */
	public static final String INTENT_PARAM_POINT_ID = "intent_param_point_id";

	/**
	 * ID of the point, which is connected with the note.
	 */
	long pointId;

	/**
	 * EditText for the note.
	 */
	EditText note_edit;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setTitle(R.string.note_title);
		this.setContentView(R.layout.note);

		Intent intent = this.getIntent();
		pointId = intent.getLongExtra(INTENT_PARAM_POINT_ID, -1);

		note_edit = (EditText) this.findViewById(R.id.note_edit);
		Button saveBtn = (Button) this.findViewById(R.id.note_btn_save);
		saveBtn.setOnClickListener(this);
		Button rejectBtn = (Button) this.findViewById(R.id.note_btn_reject);
		rejectBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.note_btn_save) {
			String note = note_edit.getText().toString();
			if (note != null && note.length() > 0)
				(new Thread(savingHelper)).start();
			this.finish();
		} else if (v.getId() == R.id.note_btn_reject) {
			this.finish();
		}
	}

	private Runnable savingHelper = new Runnable() {
		@Override
		public void run() {
			Log.d(Constants.TAG, "Saving note at point[id: " + pointId + "]");
			DataBaseConnector db = new DataBaseConnectorImpl(NoteActivity.this);
			db.addMedia(MediaController.MEDIA_TYPE_TEXT, note_edit.getText().toString(),
					pointId);
			db.close();
		}
	};
}